package io.renren.modules.live.controller;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Map;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.renren.common.exception.RRException;
import io.renren.common.utils.PageResult;
import io.renren.modules.live.entity.FanweVideoEntity;
import io.renren.modules.live.vo.AnchorVo;
import io.renren.modules.live.vo.GetOrderListTo;
import io.renren.modules.live.vo.orderVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.log4j.Log4j2;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import io.renren.modules.live.entity.FanweOrderEntity;
import io.renren.modules.live.service.FanweOrderService;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.R;

import javax.xml.crypto.Data;


/**
 * 订单表
 *
 * @author shaoqi
 * @email sunlightcs@gmail.com
 * @date 2020-11-21 13:24:54
 */
@RestController
@Api(tags = "订单管理")
@RequestMapping("order")
@Log4j2
public class FanweOrderController {
    @Autowired
    private FanweOrderService fanweOrderService;

    /**
     * 列表
     */
    @ApiOperation("获取所有订单")
    @GetMapping("/list")
    //@RequiresPermissions("live:fanweorder:list")
    public R list( @ApiParam("当前页") Long page,
                   @ApiParam("每页显示数量")Long size){
        Page<FanweOrderEntity> userPage = new Page<>(page, size);
        fanweOrderService.page(userPage,null);
        PageResult<FanweOrderEntity> result1 = new PageResult<>();
        result1.setTotal(userPage.getTotal());
        result1.setRows(userPage.getRecords());
        return R.ok("获取成功").put("pageResult",result1);
    }


    @ApiOperation("根据普通用户ID,某一天日期获取当天交易记录信息列表")
    @GetMapping("/getByUserIdDataOrderlist/{userId}")
    //@RequiresPermissions("live:fanweorder:list")
    public R getUserIdDataOrderlist(@PathVariable @ApiParam(value = "用户id",required = true) Integer userId,
                                    @ApiParam(value = "日期",required = true) Date time,
                                   @ApiParam("当前页") Long page,
                                   @ApiParam("每页显示数量")Long size){
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String scl = sdf.format(time);
        String data = scl.substring(0, 10);
        GetOrderListTo getOrderListTo = OrderUtil(userId, data,page,size);
        if (StringUtils.isEmpty(getOrderListTo)){
            return R.error("您"+time+"号还没有任何记录");
        }else {
            return R.ok("获取成功").put("orderListTo", getOrderListTo);
        }
    }

    @ApiOperation("根据普通用户ID,获取当前这个月之内交易记录信息列表")
    @GetMapping("/getByUserIdOneMonthOrderlist")
    //@RequiresPermissions("live:fanweorder:list")
    public R getUserIdDataOrderlist(@ApiParam(value = "用户id",required = true) Integer userId,
                                    @ApiParam(value = "当前页",required = true) Long page,
                                    @ApiParam(value = "每页显示数量",required = true)Long size){
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date cl=new Date();
        String scl = sdf.format(cl);
        String time = scl.substring(0, 7);
        GetOrderListTo getOrderListTo = OrderUtil(userId, time,page,size);
        if (StringUtils.isEmpty(getOrderListTo)){
            return R.error("您"+time+"月还没有任何记录");
        }else {
            return R.ok("获取成功").put("orderListTo", getOrderListTo);
        }




    }

    private GetOrderListTo OrderUtil(Integer userId,String data,Long page,Long size){
        List<FanweOrderEntity> orderList = fanweOrderService.getUserIdDataOrderlist(userId,data,page,size);
        BigDecimal payResult = new BigDecimal(0.00);//支出总金额
        BigDecimal incomeyResult = new BigDecimal(0.00);//收入总金额
        if(orderList.isEmpty()&&orderList.size()==0){
            return null;
        }else {
            for (FanweOrderEntity orderEntity : orderList) {
                if(orderEntity.getOrderType()==1){
                    payResult = payResult.add(orderEntity.getOrderMoney());
                }else {
                    incomeyResult = incomeyResult.add(orderEntity.getOrderMoney());
                }
            }
            GetOrderListTo orderListTo = new GetOrderListTo();
            orderListTo.setIncomeyResult(incomeyResult);
            orderListTo.setPayResult(payResult);
            return orderListTo;
        }
    }
    /**
     * 佟卢海 - 2020.11.27
     *
     * 充值记录
     */
    @PostMapping("/selectOrderListOne")
    @ApiOperation("管理员查看主播列表")
    @RequiresPermissions("liveAdministration:finance")
    public R selectOrderListOne(@RequestBody orderVo orderVo){
        R result=new R();
        try{
            if(orderVo.getPageIndex()==null || orderVo.getPageSize() ==null){
                result.put("code",9999);
                result.put("msg","页码页容不能为空！！");
                result.put("data",null);
                return result;
            }
            Map<String, Object> stringObjectMap = fanweOrderService.selectOrderListOne(orderVo);
            result.put("data",stringObjectMap);
        }catch (Exception e){
            log.info(e);
            result.put("code",9999);
            result.put("msg","查询失败");
            result.put("data",null);
        }
        return result;
    }
    /**
     * 佟卢海 - 2020.11.27
     *
     * 充值记录
     */
    @PostMapping("/selectOrderListTwo")
    @ApiOperation("管理员查看主播列表")
    @RequiresPermissions("liveAdministration:finance")
    public R selectOrderListTwo(@RequestBody orderVo orderVo){
        R result=new R();
        try{
            if(orderVo.getPageIndex()==null || orderVo.getPageSize() ==null){
                result.put("code",9999);
                result.put("msg","页码页容不能为空！！");
                result.put("data",null);
                return result;
            }
            Map<String, Object> stringObjectMap = fanweOrderService.selectOrderListTwo(orderVo);
            result.put("data",stringObjectMap);
        }catch (Exception e){
            log.info(e);
            result.put("code",9999);
            result.put("msg","查询失败");
            result.put("data",null);
        }
        return result;
    }

}
